package com.wl.api.controller.business;

import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.annotation.RequiresAuthentication;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.common.utils.constant.AppEnum;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerUserDTO;
import com.wl.api.model.business.CustomerUser;
import com.wl.api.service.business.ICustomerUserService;

/**
 *  PC 客户管理
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/external/customer/user")
public class CustomerUserController extends CommonController {

	@Autowired
	ICustomerUserService customerUserService;
	
	@RequestMapping(value="/manage", method= RequestMethod.POST)
	public JsonObject create(@RequestBody CustomerUserDTO param) {
		JsonObject json = JsonObject.getInstance();
		String title = "", code = "";
		boolean pass = true;
		if (validate().negate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			CustomerUser user = BeanUtils.copyProperties(param, CustomerUser.class);
			customerUserService.insert(user);
			json.setData(user.getCustId());	
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	@RequiresAuthentication(getClientType = AppEnum.customer_client)
	@RequestMapping(value="/manage/{id}", method=RequestMethod.PUT)
	public JsonObject update(@PathVariable("id")Long custId, @RequestBody CustomerUserDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO userDTO = getCommonUser();
		boolean pass = true;
		String title = "", code = "";
		if (param.getCustId() == null || !Objects.equals(userDTO.getCustId(), param.getCustId())) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		/*if (StringUtil.isNotEmpty(param.getPassword()) || StringUtil.isNotEmpty(param.getPhone())) {
			code = ErrorCodeEnum.service_not_supported.getError_code();
			title = ErrorCodeEnum.service_not_supported.getMessage();
			pass = false;
		}*/
		if (pass) {
			param.setPassword(null);
			customerUserService.updateById(BeanUtils.copyProperties(param,CustomerUser.class));
			json.setData(param.getCustId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequiresAuthentication(getClientType=AppEnum.customer_client)
	@RequestMapping(value =	"pass" ,method = RequestMethod.PUT)
	public JsonObject modifyPass(CustomerUserDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		JsonObject json  = JsonObject.getInstance();
		String title ="", code = "";
		boolean  pass = true;
		if (!Objects.equals(user.getUsername(), param.getUsername()) || StringUtil.isNotEmpty(param.getOldpass())) {
			pass = false;
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
		}
		if (pass) {
			
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	private Predicate<CustomerUserDTO> validate() {
		return s -> !StringUtil.isEmpty(s.getUsername()) 
				&& !StringUtil.isEmpty(s.getRealname()) && StringUtil.isEmail(s.getEmail())
				&& StringUtil.isNotEmpty(s.getPhone()) && StringUtil.isNotEmpty(s.getPassword());
	}
}
